package com.longway.core.toolbox.wdp.api;

import java.io.File;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.longway.common.kit.URLKit;
import com.longway.core.toolbox.utils.HttpUtils;

/**
 * 代码管理
 *
 */
public class WxaCodeApi {
    private static String url = "https://api.weixin.qq.com/wxa/commit?access_token=";
    private static String getPageurl = "https://api.weixin.qq.com/wxa/get_page?access_token=";
    private static String getQrcodeurl = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=s%&path=s%";
    private static String submitAuditurl = "https://api.weixin.qq.com/wxa/submit_audit?access_token=";
    private static String uploadmediaurl = "https://api.weixin.qq.com/wxa/uploadmedia?access_token=";

    @SuppressWarnings("unchecked")
    /**
     * 上传小程序代码
     * @param template_id
     * @param ext_json
     * @param user_version
     * @param user_desc
     * @return
     */
	public static Map<String,String> commitCode(String appId,String templateId,String extJson,String userVersion,String userDesc){
    	  //获取accessToken  
    	  String accessToken = AuthorizationInfoApi.getAuthorizationInfoAccessToken(appId);
    	  final Map<String, String> queryParas = Ret.create("template_id", templateId)
          		.set("ext_json", extJson).set("user_version", userVersion).set("user_desc", userDesc);
    	  String json = HttpUtils.post(url+accessToken,JsonKit.toJson(queryParas));
    	  return JsonKit.parse(json, Map.class);
    }
    
    /**
     * 获取已上传的代码的页面列表
     * @param appId
     * @return
     * {
		  "errcode": 0,
		  "errmsg": "ok",
		  "page_list": ["index", "page/list", "page/detail"]
		}
     */
    public static Map<String,Object> getPageList(String appId){
    	  //获取accessToken  
    	  String accessToken = AuthorizationInfoApi.getAuthorizationInfoAccessToken(appId);
    	  String json = HttpUtils.get(getPageurl+accessToken);
    	  return JsonKit.parse(json, Map.class);
    }
    
    /**
     * 获取体验版二维码
     * @param appId
     * @param path 指定二维码扫码后直接进入指定页面并可同时带上参数
     * @return 媒体文件
     */
    public static MediaFile getQrcode(String appId,String path){
  	  //获取accessToken  
  	  String accessToken = AuthorizationInfoApi.getAuthorizationInfoAccessToken(appId);
  	  getQrcodeurl = String.format(getQrcodeurl, accessToken,URLKit.encode(path, "utf-8"));
  	  return HttpUtils.download(getQrcodeurl);
    }
    
    /**
     * 提交审核
     * @param appId
     * @return
     * {
		  "errcode": 0,
		  "errmsg": "ok",
		  "auditid": 1234567
		}
     */
    public static Map<String,Object> submitAudit(String appId,String para){
  	  //获取accessToken  
  	  String accessToken = AuthorizationInfoApi.getAuthorizationInfoAccessToken(appId);
  	  String json = HttpUtils.post(submitAuditurl+accessToken,para);
  	  return JsonKit.parse(json, Map.class);
    }
    
    /**
     * 提审素材上传接口介绍
     * @param appId
     * @param file
     * @param params
     * @return
     * {
			"errcode":0,
			"errmsg":"ok",
			"type":"image",
			"mediaid":"xxxxxxxxxxxxxxxxx"
		}
     */
    public static Map<String,Object> uploadMedia(String appId,File file,String params){
    	  //获取accessToken  
    	  String accessToken = AuthorizationInfoApi.getAuthorizationInfoAccessToken(appId);
    	  String json = HttpUtils.upload(uploadmediaurl+accessToken, file,params);
    	  return JsonKit.parse(json, Map.class);
      }
    
    
}
